package com.zbl.zblproject.core.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zbl.zblproject.core.entity.Order;
import com.zbl.zblproject.core.entity.OrderDTO;
import com.zbl.zblproject.core.entity.OrderItem;
import com.zbl.zblproject.erp.entity.ErpTempOrderItem;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author youlanqiang
 * @version 1.0
 * @date 2018/10/11
 */
@Repository
public interface OrderItemMapper extends BaseMapper<OrderItem> {

    @Update("UPDATE zbl_orderitem SET suppliers_id = #{suppliersId} WHERE id = #{id} ")
    boolean updateOrderItemSuppliers(@Param("id") int id, @Param("suppliersId") int suppliersId);

    void updateItemsNopurByIds(@Param("ids") List<Integer> ids);

    @Select("SELECT MIN(id) FROM  zbl_orderitem WHERE order_sn = #{sn}")
    Integer selectMinId(@Param("sn") String orderSn);

    Integer selectGoodsNumber(@Param("orderItemId") int orderItemId);

    @Select("SELECT order_sn FROM zbl_orderitem WHERE id = #{id}")
    String selectOrderSn(@Param("id") int itemId);

    @Select("SELECT * FROM zbl_orderitem WHERE is_out = 0 AND is_into = 0 AND order_sn = #{orderSn}")
    List<OrderItem> selectIdsByNotOutAndIn(@Param("orderSn") String orderSn);

    @Delete("DELETE FROM zbl_orderitem WHERE order_sn = #{orderSn}")
    void deleteOrderSn(@Param("orderSn") String orderSn);

    /**
     * 更新所有未出库的orderItem
     *
     * @param shopPrice 单价
     * @param brandName 品牌名称
     * @param desc      备注
     * @param goodsName 商品名称
     * @param goodsId   商品ID
     * @return
     */
    boolean updateOrderItemByUp(@Param("shopPrice") BigDecimal shopPrice, @Param("brandName") String brandName,
                                @Param("goodsDesc") String desc, @Param("goodsName") String goodsName, @Param("goodsId") int goodsId);


    /**
     * 查询订单的商品数量
     *
     * @param orderSn 订单号
     * @return
     */
    Integer selectOriginOrderNumber(@Param("orderSn") String orderSn);

    Integer selectOrderOutNumber(@Param("orderSn") String orderSn);

    /**
     * 查询订单的出库商品数量
     *
     * @param mode
     * @param status
     * @return
     */
    Integer selectOutOrderNumberByOrigin(@Param("originOrderSn") String originOrder,
                                         @Param("orderId") int orderId,
                                         @Param("mode") int mode,
                                         @Param("status") int status);


    List<ErpTempOrderItem> selectErpTempOrderItemByOrderSn(@Param("orderSn") String orderSn, @Param("isOut") int isOut);


    List<ErpTempOrderItem> selectErpTempOrderItemByOrderSnAndPurch(@Param("orderSn") String orderSn, @Param("isPurch") int isPurch);

    List<ErpTempOrderItem> selectErpTempOrderItemByOrderSnAndIsInto(@Param("orderSn") String orderSn, @Param("isInto") int isInto);

    @Select("SELECT count(*) FROM zbl_orderitem WHERE order_sn = #{orderSn} AND is_out = #{isOut} ")
    Integer selectOrderSnAndIsOutSum(@Param("orderSn") String orderSn, @Param("isOut") int isOut);

    @Select("SELECT SUM(shop_price * goods_num) FROM zbl_orderitem WHERE order_sn = #{orderSn}")
    BigDecimal selectOrderTotal(@Param("orderSn") String orderSn);


    List<OrderItem> selectCgddOrderItemByMonth(@Param("month") String month);

    @Update("UPDATE zbl_orderitem  SET  is_into = 0, is_out = 0  WHERE id = #{orderId}")
    boolean setOrderIntoAndOutForZero(@Param("orderId") int orderId);

    @Select("SELECT IFNULL(COUNT(*), 0) FROM zbl_orderitem WHERE order_sn = #{orderSn} AND is_out = 0 ")
    Integer selectCountByIsOut(@Param("orderSn") String orderSn);

    @Select("SELECT * FROM zbl_orderitem WHERE order_sn = #{orderSn} AND is_out = 0 ")
    List<OrderItem> selectListByIsOut(@Param("orderSn") String orderSn);


    @Select("SELECT * FROM zbl_orderitem WHERE order_sn = #{orderSn} LIMIT 1")
    OrderItem selectForOne(@Param("orderSn") String orderSn);

    @Select("SELECT * FROM zbl_orderitem WHERE order_sn = #{orderSn} and goods_id=#{goodsId}")
    OrderItem selectBySnAndGoodsId(@Param("orderSn") String orderSn, @Param("goodsId") int goodsId);

    @Update("update zbl_orderitem set is_into=#{IsInTo} where order_sn=#{orderSn} and is_into!=#{IsInTo}")
    int updateIsInTo(@Param("orderSn") String orderSn, @Param("IsInTo") int IsInTo);

    @Update("update zbl_orderitem set is_out=#{IsInTo} where order_sn=#{orderSn}")
    int updateIsOut(@Param("orderSn") String orderSn, @Param("IsInTo") int IsInTo);

    @Select("select sum(goods_num) as goods_num,id,order_sn,carts_id,goods_id,goods_name,brand_name," +
            "goods_desc,shop_price,suppliers_id,discount from zbl_orderitem where id in(${ids}) group by goods_name")
    List<OrderItem> getGoodsName(@Param("ids") String ids);

    List<Order> selectAllOrderItem(@Param("orderSn") String orderSn, @Param("supId") String supId);

    List<OrderItem> selectByIdOrderItem(@Param("id") String id);

    @Delete("DELETE FROM zbl_orderitem WHERE id in(${id})")
    void deleteOrderItem(@Param("id") String id);

    int updateOrderItemNum(OrderItem orderItem);

    @Select("SELECT b.goods_id FROM zbl_erp_order a LEFT JOIN zbl_erp_orderitem " +
            "b ON a.order_info = b.order_sn where a.`status`=2 and DATE_FORMAT(a.create_time,'%Y-%m')='2019-05' GROUP BY b.goods_id order by b.suppliers_id asc")
    List<Integer> getGoodsId();

    OrderDTO get(@Param("id")int id);

    @Select("select order_sn from zbl_orderitem where not like 'CG%' and like '20190906%' group by suppliers_id having count(*)>1")
    List<String> getString();

}
